import EventRoute from "./EventRoute"
import {BrowserWindow} from "electron";

import Session from "../session/Session";
import systemUserRoutes from "./SystemUserRoutes";
import SystemMemberGradeService from "../service/SystemMemberGradeService";
import SystemProductCategoryService from "../service/SystemProductCategoryService";
import systemMemberRoutes from "./SystemMemberRoutes";
import systemProductCategoryRoutes from "./SystemProductCategoryRoutes";
import systemMemberGradeRoutes from "./SystemMemberGradeRoutes";
import systemProductRoutes from "./SystemProductRoutes";
import systemCardRoutes from "./SystemCardRoutes";
import systemLoyaltyRoutes from "./SystemLoyaltyRoutes";
import systemGlobalRoutes from "./SystemGlobalRoutes";

let routers = new Array();

const systemMemberGradeService = new SystemMemberGradeService();
const systemProductCategoryService = new SystemProductCategoryService();

/**
 * 登录成功
 */
routers.push(
    new EventRoute('app-login-success', 'event', (api, data = {}) => {
        // 隐藏登录窗体
        api.windows['login'].hide()
        // 显示主窗体
        const mainWindow = api.windows['main']
        mainWindow.show()

        setInterval(() => {
            const session = Session.getInstance()
            mainWindow.getWebContents().send('main-to-renderer', {
                name: 'app-auth-principal',
                event: 'event',
                data: session.getPrincipal()
            })
        }, 2000)
    })
)

routers = routers.concat(systemMemberGradeRoutes) //会员等级
routers = routers.concat(systemProductCategoryRoutes) //产品分类
routers = routers.concat(systemUserRoutes) //操作员
routers = routers.concat(systemGlobalRoutes) //全局设置
routers = routers.concat(systemMemberRoutes) //会员管理
routers = routers.concat(systemCardRoutes) //会员卡
routers = routers.concat(systemLoyaltyRoutes) //积分卡
routers = routers.concat(systemProductRoutes) //库存管理

// 打开外部URL
routers.push(
  new EventRoute('open-external-url', 'event', (api, data = {}) => {
    const url = data.data
    api.shell.openExternal(url)
  })
)

/**
 * 最小化应用
 */
routers.push(
  new EventRoute('app-minimize', 'event', (api, data = {}) => {
    const windows = BrowserWindow.getAllWindows()
    for (let i = 0; i < windows.length; i++) {
      windows[i].minimize()
    }
  })
)

/**
 * 退出应用
 */
routers.push(
  new EventRoute('app-quit', 'event', (api, data = {}) => {
    api.app.quit()
  })
)

export default routers
